package org.ntu.csn.doraemon.sprite;
import java.util.Random;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import org.ntu.csn.doraemon.canvas.Main;
import org.ntu.csn.doraemon.model.GameObject;
import org.ntu.csn.doraemon.tool.Constant;


public class Lifes extends GameObject {

	
	
	public Lifes(Image img, int maxLife, int maxFairyWater, int picwidth,int picheight,int width,int height) {
	   
		super(img,picwidth,picheight);
		int max = maxLife + maxFairyWater;
		this.currentLife = maxLife;
	    lifes=new int[max][6];
	    this.width=width;
	    this.height=height;
	    this.pictureWidth = picwidth;
	}

  public void initLifes(){
    for (int i = 0; i < lifes.length; i++) {
      initLife(i);
    }
  }

  private void initLife(int i) {
    lifes[i][ALIVE] = 1; //alive
    if(i==0)
    	lifes[i][X] = 160;
    else
    	lifes[i][X] = lifes[i -1][X] + this.pictureWidth;//(rnd.nextInt() & 0x7fffffff) % width;
    lifes[i][Y] = 270;//(rnd.nextInt() & 0x7fffffff) % (height/4); //-5;
    //System.out.println(lifes[i][X]);
  }


  private void paint(Graphics g,int i){
    updataspritepos(i);
    sprite.paint(g);
  }

  public void paint(Graphics g) {
    for (int i = 0; i < this.currentLife; i++) {
      if(lifes[i][ALIVE]==0){
        continue;
      }
      sprite.setPosition(lifes[i][X],lifes[i][Y]);
      sprite.paint(g);
    }
  }

  private void updataspritepos(int i){
    sprite.setPosition(lifes[i][1],lifes[i][2]);
  }
  
  public void checkForCurrentLife(){
	  for(int i=0; i<currentLife; i++){
		  lifes[i][ALIVE]=1;
	  }
	  for (int i = currentLife; i < lifes.length; i++) {
		    lifes[i][ALIVE]=0;
	  }
  }
  
  public void setCurrentLife(int leftLife){
	  this.currentLife = leftLife;
  }
  
	private int[][] lifes;
	private int width,height;

	private int X = 1;
	private int Y = 2;
	private int ALIVE = 5;
	
	private int currentLife;
	private int pictureWidth;
	
}